<?php

class Mod_Products extends CI_Model{
		
	public function getProducts($start, $num){
		$this->db->select('*');
        $this->db->limit($num, $start);
		$this->db->order_by('id', 'DESC');
		return $this->db->get(table('product'));
	}
	
    public function getProductNum(){
        $objDb = $this->getAllProducts();
        return $objDb->num_rows();
    }
	
    public function getAllProducts() {
        $this->db->select('*');
        $this->db->from(table('product'));
		return $this->db->get();
    }
	
    public function getProductById($id) {
        $this->db->select('*');
        $this->db->where(field('id'),$id);
		$result = $this->db->get(table('product'));
		$result = $result->result_array();
		return $result[0];
    }
	
	public function insert($image, $title, $description, $broken, $shipping, $mini_order, $payment, $fob_price){
		$data = array(
			field('image') => $image,
			field('name') => $title,
			field('brokenRate') => $broken,
			field('shipping') => $shipping,
			field('miniOrder') => $mini_order,
			field('paymentTerm') => $payment,
			field('fobPrice') => $fob_price,
			field('description') => $description
		);			
		$result = $this->db->insert(table('product'), $data); 
		return $this->db->affected_rows($result);
	}
	
	public function update($id, $image, $title, $description, $broken, $shipping, $mini_order, $payment, $fob_price){
		$data = array(
			field('image') => $image,
			field('name') => $title,
			field('brokenRate') => $broken,
			field('shipping') => $shipping,
			field('miniOrder') => $mini_order,
			field('paymentTerm') => $payment,
			field('fobPrice') => $fob_price,
			field('description') => $description
		);			
		if(empty($image)) unset($data[field('image')]);
		
		$result = $this->db->update(table('product'), $data, field('id') . ' = '. $id); 
		return $this->db->affected_rows($result);
	}
	
    public function delete($id) {
        $this->db->where(field('id'), $id);
        $result = $this->db->delete(table('product'));
		return $this->db->affected_rows($result);
    }
}